package com.icss.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.icss.beans.JobInfoVO;
import com.icss.beans.User;
import com.icss.utils.RecordSet;

/**
 * @author 韦锦良
 *　类的作用：就业信息逻辑处理类
 * 最后修改时间：2010-3-25　下午01:04:28
 * 最后的改动内容：初始版本
 */
public class JobInfoDao {

	private ResultSet rs = null;

	private Dao dao = new Dao();

	//通过用户的编号获得所有的就业信息
	public ArrayList<JobInfoVO> geJobInfoVOsByUid(int uid) {

		ArrayList<JobInfoVO> vos = null;

		String sql = "select * from jobInfo where uid=" + uid;

		ResultSet rs = dao.query(sql);

		vos = getVOFromResultSet(rs);
		close();
		return vos;

	}
	
	//为管理员角色获得所有的就业信息
	public ArrayList<JobInfoVO> getAllJobInfoForAdmin() {

		String sql = "select * from JobInfo order by jobInfoID desc";

		ArrayList<JobInfoVO> jobInfoVOs = null;

		ResultSet rs = dao.query(sql);

		jobInfoVOs = getVOFromResultSet(rs);

		close();
		return jobInfoVOs;
	}

	//为管理员获得所有就业信息，这里是实现分布的模式
	public RecordSet getInfoForAdmin(int pageSize, int currentPage, String type, String typeArg) {

		RecordSet set = null;
		StringBuffer strSQL = null;
		if ("".equals(typeArg) && type != null) {
			return set;
		}
		if ("byName".equals(type)) {

			strSQL = new StringBuffer(
					"select * from JobInfo j inner join users u on j.uid = u.uid where u.userName like '%");
			strSQL.append(typeArg).append("%' order by jobInfoID desc");
		} else if ("byCompany".equals(type)) {

			strSQL = new StringBuffer(
					"select * from JobInfo j inner join users u on j.uid = u.uid  where j.workingCompany like '%");
			strSQL.append(typeArg).append("%' order by jobInfoID desc ");
		} else if ("byClass".equals(type)) {

			strSQL = new StringBuffer(
					"select * from JobInfo j inner join users u on j.uid = u.uid inner join stuClass s on u.stuClassId = s.stuClassId  where s.ClassName like '%");
			strSQL.append(typeArg).append("%' order by jobInfoID desc ");
		} else if (type == null) {

			strSQL = new StringBuffer(
					"select * from JobInfo j inner join users u on j.uid = u.uid inner join stuClass s on u.stuClassId = s.stuClassId order by jobInfoID desc");
		}

		set = dao.queryPager(strSQL.toString(), pageSize, currentPage);
		return set;
	}

	//为主页获得所有的就业信息
	public RecordSet getInfoForIndex() {

		RecordSet rs = null;
		String strSQL = "select * from JobInfo j inner join users u on j.uid = u.uid  order by jobInfoID desc";
		rs = dao.queryPager(strSQL, 8, 1);
		close();
		return rs;
	}

	//将ResultSet里面的数据封装到vo类里面去
	public ArrayList<JobInfoVO> getVOFromResultSet(ResultSet rs) {

		ArrayList<JobInfoVO> vos = new ArrayList<JobInfoVO>();

		try {
			while (rs.next()) {
				JobInfoVO vo = new JobInfoVO();
				UserDao userDao = new UserDao();
				User user = userDao.getUserInfo(rs.getInt("uid"));
				vo.setJobInfoID(rs.getInt("jobInfoId"));
				vo.setSalary(rs.getDouble("salary"));
				vo.setWorkingCompany(rs.getString("workingCompany"));
				vo.setLeaveCompanyTime(rs.getDate("leaveCompanyTime"));
				vo.setEnterCompanyTime(rs.getDate("enterCompanyTime"));
				vo.setUser(user);
				vo.setVisiable(rs.getByte("visiable"));
				vos.add(vo);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return vos;

	}

	//通过就业编号获得就业信息
	public JobInfoVO getJobInfoVOById(String jobInfoID) {

		JobInfoVO jobInfo = null;

		String sql = "select * from jobInfo where jobInfoID=" + jobInfoID;

		ResultSet rs = dao.query(sql);

		jobInfo = getVOFromResultSet(rs).get(0);

		close();

		return jobInfo;
	}

	//为前台获得就业信息
	public ArrayList<JobInfoVO> getJobInfoForFoward() {

		String sql = "select * from JobInfo where visiable=1";

		ArrayList<JobInfoVO> vos = null;

		ResultSet rs = dao.query(sql);

		vos = getVOFromResultSet(rs);

		close();

		return vos;

	}

	public void close() {

		try {
			if (null != rs) {
				rs.close();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		dao.close();
	}

	public boolean execute(String sql) {
		return dao.execute(sql);
	}
}
